﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		AbstactTruncatedString.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 //   File:            AbstactTruncatedString.cs
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System;
using System.Data;
using NHibernate.SqlTypes;
using NHibernate.Type;

namespace Dexter.Repository.NHibernate.UserTypes {

	[Serializable]
	internal abstract class AbstractTruncatedStringType : AbstractStringType {
		internal AbstractTruncatedStringType ( )
			: base ( new StringSqlType ( ) ) {}

		/// <summary>
		/// Initializes a new instance of the <see cref="AbstractTruncatedStringType"/> class.
		/// </summary>
		/// <param name="sqlType">Type of the SQL.</param>
		internal AbstractTruncatedStringType ( StringSqlType sqlType )
			: base ( sqlType ) {}

		/// <summary>
		/// Gets the length.
		/// </summary>
		/// <value>The length.</value>
		public abstract int Length { get; }

		/// <summary>
		/// Sets the specified CMD.
		/// </summary>
		/// <param name="cmd">The CMD.</param>
		/// <param name="value">The value.</param>
		/// <param name="index">The index.</param>
		public override void Set ( IDbCommand cmd , object value , int index ) {
			var str = ( string ) value;
			if ( str.Length > Length ) {
				str = str.Substring ( 0 , Length );
			}
			base.Set ( cmd , str , index );
		}
	}
}
